﻿using System;
using System.Windows.Forms;
using Dapper;
using ExtendLib.Helper;
using InterfaceLib;

namespace SmsBomb
{
    public partial class ConfigMgr : Form
    {
        public ConfigMgr()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 默认绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfigMgr_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNew_Click(object sender, EventArgs e)
        {
            ConfigAdd ca = new ConfigAdd();
            ca.ShowDialog();
            if (ca.DialogResult == DialogResult.OK)
            {
                DataBind();
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选中后再操作！");
                return;
            }
            ConfigAdd ca = new ConfigAdd { KeyId = int.Parse(dgvList.SelectedRows[0].Cells[0].Value.ToString()) };
            ca.ShowDialog();
            if (ca.DialogResult == DialogResult.OK)
            {
                DataBind();
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void DataBind()
        {
            var list = DbHelperByDapper.GetList<MsgModel>(new { });
            dgvList.DataSource = list;
        }
        /// <summary>
        /// 验证配置信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选中后再操作！");
                return;
            }
            var check = new mainTest { KeyId = int.Parse(dgvList.SelectedRows[0].Cells[0].Value.ToString()) };
            check.ShowDialog();
        }
        /// <summary>
        /// 双击删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var keyId = int.Parse(dgvList.SelectedRows[0].Cells[0].Value.ToString());
            var dr = MessageBox.Show(string.Format("是否确认删除【{0}】",keyId), "选择", MessageBoxButtons.YesNo);
            if (DialogResult.Yes == dr)
            {
                int r = DbHelperByDapper.DeleteDb<MsgModel>(keyId);
                if (r > 0)
                {
                    MessageBox.Show("删除成功！");
                    DataBind();
                }
                   
            }
        }
        /// <summary>
        /// 一键废弃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            using (var conn=DbHelperByDapper.OpenConnection())
            {
                string cmd = "Update Config Set [Status]='废弃'";
                int r=conn.Execute(cmd);
                if (r > 0)
                {
                    MessageBox.Show("全部删除成功！");
                    DataBind();
                }
            }
        }
        /// <summary>
        /// 一键正常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            using (var conn = DbHelperByDapper.OpenConnection())
            {
                string cmd = "Update Config Set [Status]='正常'";
                int r = conn.Execute(cmd);
                if (r > 0)
                {
                    MessageBox.Show("全部恢复成功！");
                    DataBind();
                }
            }
        }

    }
}
